Testing Library
https://testing-library.com/img/octopus-64x64.png
https://testing-library.com/
Component のテストを行うためのライブラリ群
React や Vue、Svelte など様々なフレームワークに適したライブラリを提供している
導入(React + Vitest)
https://testing-library.com/docs/react-testing-library/setup#using-without-jest
インストール
code:sh
$ npm install --save-dev @testing-library/react happy-dom @testing-library/jest-dom
happy-dom
https://github.com/capricorn86/happy-dom
DOM API をシミュレートするライブラリ
React のコードを実行するには DOM API が必要だが、テストライブラリの実行環境である Node.js には DOM API が存在しないため必要
jsdom の代替であり、より新しく性能も良い
https://github.com/capricorn86/happy-dom/wiki/#performance
@testing-library/jest-dom
https://testing-library.com/docs/ecosystem-jest-dom/
テスト内で DOM の状態に関するアサーションを行えるようにするライブラリ
Vitest の設定
defineConfig に渡すオブジェクトの test.environment と test.setupFiles を指定する
code:vite.config.ts
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
// https://vitejs.dev/config/
export default defineConfig({
plugins: react(),
test: {
globals: true,
environment: "happy-dom",
setupFiles: "test-setup.ts",
},
});
各テストで import する手間を省くため、setupFiles に指定したファイルでは、DOM アサーションのマッチャをインストールする
https://github.com/testing-library/jest-dom?tab=readme-ov-file#with-vitest
code:test-setup.ts
import "@testing-library/jest-dom/vitest";
TypeScript のコンパイラにも認識させるため、tsconfig.json の compilerOptions.types と include を修正する
code:tsconfig.json
{
"compilerOptions": {
...
"types": "vitest/globals", "@testing-library/jest-dom"
},
"include": "src", "./test-setup.ts"
}
サンプルコード
code:CarouselButton.tsx
const CarouselButton = () => <button />;
export default CarouselButton;
code:CarouselButton.test.tsx
import { render, screen } from "@testing-library/react";
import CarouselButton from "./CarouselButton";
describe("CarouselButton", () => {
it("renders a <button>", () => {
render(<CarouselButton />);
expect(screen.getByRole("button")).toBeInTheDocument();
});
});
API
render: https://testing-library.com/docs/react-testing-library/api#render
React Component を受け取り、DOM にレンダリングする
シミュレートされた DOM は自動でテスト毎にリセットされる
screen.getByRole: https://testing-library.com/docs/queries/byrole
指定された ARIA ロール を持つ DOM 要素を 1 つ見つけ出す
要素が見つからない場合や複数見つかる場合はエラーを投げる
jest-dom
toBeInTheDocument: https://github.com/testing-library/jest-dom?tab=readme-ov-file#tobeinthedocument
DOM 要素が HTML ドキュメント内に存在するかチェックするアサーション
act: https://testing-library.com/docs/svelte-testing-library/api/#act
コンポーネントの状態が変更されたとき、その変更を DOM に反映するよう React に指示する関数
後述する user-event では自動的に呼び出されるので不要
e.g. setTimeout など タイマー を用いたテスト
カスタムフックを使った機能拡張#66d9540b75d04f0000065c5e
ユーザの インタラクション をテストする
追加で user-event ライブラリが必要
https://testing-library.com/docs/user-event/intro
code:sh
$ npm install --save-dev @testing-library/user-event
使用例
状態を持つ React Component を TDD で実装する
ESLint の設定
warning.icon Flat Config には対応しているが、 ESLint v9 には未対応
https://github.com/testing-library/eslint-plugin-testing-library/issues/924#issuecomment-2284059071
eslint-plugin-testing-library プラグインを導入する
https://testing-library.com/docs/ecosystem-eslint-plugin-testing-library/
https://github.com/testing-library/eslint-plugin-testing-library
インストール
code:sh
$ npm install --save-dev eslint-plugin-testing-library